package web.stu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import web.stu.common.ErrorCode;
import web.stu.config.Result;
import web.stu.exception.BusinessException;
import web.stu.model.dto.teacherCourse.AddTeacherCourseDTO;
import web.stu.model.dto.teacherCourse.QueryTeacherCourseDTO;
import web.stu.model.entity.User;
import web.stu.model.enums.UserRoleEnum;
import web.stu.model.vo.TeacherCourseVO;
import web.stu.service.TeacherCourseService;
import web.stu.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 教师授课管理控制器
 * 
 * <p>提供教师与课程、班级关联关系的管理功能，包括教师授课记录的增删改查。</p>
 * 
 * <p>所有操作都需要管理员权限。</p>
 * 
 * @author 系统开发者
 * @version 1.0
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/teach")
@Api(tags = "教师班级授课接口")
public class TeachCourseController {

    @Resource
    private UserService userService;

    @Resource
    private TeacherCourseService teacherCourseService;

    /**
     * 查询教师班级授课记录
     *
     * @param queryTeacherCourseDTO
     * @param request
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "pageTeacherCourse", notes = "分页获取教师授课记录")
    public Result<Page<TeacherCourseVO>> pageTeacherCourse(@RequestBody QueryTeacherCourseDTO queryTeacherCourseDTO, HttpServletRequest request) {
        // 非空判断
        if (queryTeacherCourseDTO == null) {
            throw new BusinessException(ErrorCode.PRIMARY_ERROR);
        }
        // 权限校验
        User loginUser = userService.getLoginUser(request);
        if (!userService.isCompareRole(loginUser, UserRoleEnum.ADMIN.getKey())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "仅管理员可用");
        }
        Page<TeacherCourseVO> teacherCourseVOPage = teacherCourseService.pageTeacherCourses(queryTeacherCourseDTO, request);
        return Result.success(teacherCourseVOPage);
    }


    /**
     * 添加教师授课记录
     *
     * @param addTeacherCourseDTO
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "addTeachClassCourse", notes = "添加教师授课记录")
    public Result<TeacherCourseVO> addTeachClassCourse(@RequestBody AddTeacherCourseDTO addTeacherCourseDTO, HttpServletRequest request) {
        // 非空判断
        if (addTeacherCourseDTO == null) {
            throw new BusinessException(ErrorCode.PRIMARY_ERROR);
        }
        // 权限校验
        User loginUser = userService.getLoginUser(request);
        if (!userService.isCompareRole(loginUser, UserRoleEnum.ADMIN.getKey())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "仅管理员可用");
        }
        TeacherCourseVO teacherCourseVO = teacherCourseService.addTeacherCourse(addTeacherCourseDTO, request);
        return Result.success(teacherCourseVO);
    }

    /**
     * 根据课程id和班级id 修改教师授课记录
     *
     * @param addTeacherCourseDTO
     * @param request
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "addTeachClassCourse", notes = "修改教师授课记录")
    public Result<TeacherCourseVO> updateTeachClassCourse(@RequestBody AddTeacherCourseDTO addTeacherCourseDTO, HttpServletRequest request) {
        // 非空判断
        if (addTeacherCourseDTO == null) {
            throw new BusinessException(ErrorCode.PRIMARY_ERROR);
        }
        // 权限校验
        User loginUser = userService.getLoginUser(request);
        if (!userService.isCompareRole(loginUser, UserRoleEnum.ADMIN.getKey())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "仅管理员可用");
        }
        TeacherCourseVO teacherCourseVO = teacherCourseService.updateTeacherCourse(addTeacherCourseDTO, request);
        return Result.success(teacherCourseVO);

    }


    /**
     * 删除教师授课记录
     *
     * @param addTeacherCourseDTO 包含课程ID和班级ID的DTO对象
     * @param request HTTP请求对象
     * @return 操作结果
     * @throws BusinessException 如果参数为空或用户无权限
     * @apiNote 此操作需要管理员权限，且会级联删除相关的成绩记录
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "deleteTeacherCourse", notes = "删除教师授课记录")
    public Result<Void> deleteTeacherCourse(@RequestBody AddTeacherCourseDTO addTeacherCourseDTO, HttpServletRequest request) {
        // 非空判断
        if (addTeacherCourseDTO == null) {
            throw new BusinessException(ErrorCode.PRIMARY_ERROR);
        }
        teacherCourseService.deleteTeacherCourse(addTeacherCourseDTO, request);
        return Result.success();
    }


}
